from typing import List, Tuple
import torch
from word_vec_encoders.mini_sequence_conv_encoder import MiniSequenceConvEncoder
from utils.handian_data import HandianData


class StrokeWordVecEncoder(MiniSequenceConvEncoder):
    """
    该编码器将字符转化为笔画串，截断或补充到固定长度并用卷积和池化提取信息作为字符向量。
    """

    def __init__(self, max_stroke_num:int = 15, embedding_dim:int = 32) -> None:
        self.max_stroke_num = max_stroke_num
        # 笔画字符表，包括1-5的数字，以及末尾填充用的0
        stroke_symbols = [str(i) for i in range(6)]
        super().__init__(name = 'strokes', miniseq_symbols = stroke_symbols, fixed_miniseq_len = max_stroke_num, embedding_dim = embedding_dim)

    def sentence_to_miniseqs_cached(self, sentence:str) -> list:
        if sentence in self._sentence_miniseq_cache:
            return self._sentence_miniseq_cache[sentence]
        sentence_miniseqs = []
        for char in sentence:
            miniseq = HandianData.get_strokes_seq_cached(char, self.max_stroke_num)
            if len(miniseq) > self.max_stroke_num:
                miniseq = miniseq[:self.max_stroke_num + 1]
            miniseq = miniseq[:15].lower()
            sentence_miniseqs.append(miniseq)
        self._sentence_miniseq_cache[sentence] = sentence_miniseqs
        return sentence_miniseqs